package com.qkl.xiatang.app.dto.log;

import com.qkl.xiatang.app.entity.feed.FeedAllocCheckEntity;
import com.qkl.xiatang.app.utils.Constants;
import com.qkl.xiatang.app.utils.appenum.FeedAllocStatusEnum;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Data
public class JobLogVO {

    private static final String DEFAULT_FILL = "无";

    private String feedAllocId;

    private String pondId;

    private String allocDate;

    private String pondName;

    private String reportMemo;


    private List<JobLogFeedVO> jobLogFeedList;

    private List<JobLogZooVO> jobLogZooList;

    private List<FeedAllocCheckEntity> jobLogCheckList;

    private static String defaultFill(String string){
        if(StringUtils.isEmpty(string)){
            return DEFAULT_FILL;
        }
        return string;
    }

    public Map<String,Object> toMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("pondName",pondName);
        //map.put("allocDate",allocDate);
        map.putAll(toFeedMap());
        return map;
    }

    private Map<String,Object> toFeedMap() {
        String strLine = Constants.ENTER_LINE;
        Map<String, Object> map = new HashMap<>();
        Map<String,JobLogFeedVO> allocUserMap = new TreeMap<>();
        String abnorMemo = "";
        String memo = "";
        if(StringUtils.isNotEmpty(reportMemo)){
            abnorMemo = reportMemo;
        }
        List<String> weighTime = new ArrayList<>();
        List<String> feedTime = new ArrayList<>();
        List<String> qty = new ArrayList<>();

        List<String> checkTime = new ArrayList<>();
        List<String> separTime = new ArrayList<>();
        List<String> shrimpFeedQty = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(jobLogFeedList)) {
            for (int i = 0 ,size = jobLogFeedList.size(); i< size ;i++) {
                JobLogFeedVO feedVO = jobLogFeedList.get(i);
                String line = strLine;
                if(i == size -1){
                    line = "";
                }
                if(StringUtils.isNotEmpty(feedVO.getMemo())){
                    memo = feedVO.getMemo();
                }
                //String serialName = feedVO.getSerialName();
                String feedName = feedVO.getFeedNameLog();
                Double feedQty = feedVO.getQty();
                Integer status = feedVO.getStatus();
                String statusMemo = "";
                if(FeedAllocStatusEnum.CANCEL.getStatus().equals(status)){
                    statusMemo = " 已取消";
                }
                feedQty = null == feedQty? 0: feedQty;
                //qty = qty  + feedName + feedQty + "斤" + line;
                qty.add( defaultFill(feedName + feedQty + "斤"));

                String voCheckTime = feedVO.getSerialName() + " " + feedVO.getCheckTime();
                String voSeparTime = feedVO.getSeparTime()+ "分钟";
                String voShrimpFeedQty = feedVO.getShrimpFeedQty();

                //feedTime = feedTime + feedVO.getFeedTime() + statusMemo + line;
                feedTime.add( defaultFill(feedVO.getFeedTime() + statusMemo));
                //weighTime = weighTime  + feedVO.getWeighTime() + line;
                weighTime.add( defaultFill(feedVO.getWeighTime()));
                //checkTime = checkTime  + voCheckTime + line;
                checkTime.add( defaultFill(voCheckTime));
                //separTime = separTime  + voSeparTime + line;
                separTime.add( defaultFill(voSeparTime));
                //shrimpFeedQty = shrimpFeedQty + voShrimpFeedQty + line;
                shrimpFeedQty.add( defaultFill(voShrimpFeedQty));

                String allocUserId = feedVO.getAllocUserId();
                if( !allocUserMap.containsKey(allocUserId)){
                    JobLogFeedVO vo = new JobLogFeedVO();
                    vo.setAllocUserName(feedVO.getAllocUserName());
                    vo.setSerialName(feedVO.getSerialName());
                    vo.setSort(feedVO.getSort());
                    allocUserMap.put(allocUserId,vo);
                }else {
                    JobLogFeedVO vo = allocUserMap.get(allocUserId);
                    vo.setSerialName(vo.getSerialName() + "、" + feedVO.getSerialName());
                }
            }
        }
        /**
        CheckDTO checkDTO = this.getCheckInfo();
        if( null != checkDTO ){
            checkTime = checkDTO.getCheckTimeList();
            separTime = checkDTO.getSeparTimeList();
            shrimpFeedQty = checkDTO.getShrimpFeedQtyList();
        }
         **/
        map.put("weighTime", weighTime);
        map.put("feedTime", feedTime);
        map.put("qty", qty);

        map.put("checkTime", checkTime);
        map.put("separTime", separTime);
        map.put("shrimpFeedQty", shrimpFeedQty);

        map.put("abnorMemo", abnorMemo);
        map.put("memo", memo);

        List<JobLogFeedVO> allocUserList = new ArrayList<>();
        allocUserList.addAll(allocUserMap.values());
        int size = allocUserList.size();
        if(size == 0){
            this.putAllocDate(map,"");
        }else if(size == 1){
            String allocUserName = allocUserList.get(0).getAllocUserName() + strLine;
            this.putAllocDate(map,allocUserName);
        }else {
            String allocUserName = "";
            allocUserList.sort(new Comparator<JobLogFeedVO>() {
                @Override
                public int compare(JobLogFeedVO o1, JobLogFeedVO o2) {
                    if(null == o1.getSort() || null == o2.getSort()){
                        return 0;
                    }
                    return o1.getSort() - o2.getSort();
                }
            });
            for (JobLogFeedVO vo : allocUserList){
                allocUserName = allocUserName + vo.getSerialName() + " " + vo.getAllocUserName() + strLine;
            }
            this.putAllocDate(map,allocUserName);
        }

        return map;
    }
    private void putAllocDate(Map<String, Object> map,String allocUserName){
        map.put("allocDate",allocUserName + allocDate);
    }
    //查找喂料时间
    private Date findFeedTime(Integer serialNo){
        if(null == serialNo || ObjectUtils.isEmpty(this.jobLogFeedList)){
            return null;
        }
        for(JobLogFeedVO feedVO : this.jobLogFeedList){
            if(serialNo.equals(feedVO.getSerialNo())){
                return feedVO.getFeedDateTime();
            }
        }
        return null;
    }

    private CheckDTO getCheckInfo(){
        CheckDTO checkDTO = null;
        if(ObjectUtils.isNotEmpty(this.jobLogCheckList)){
            checkDTO = new CheckDTO();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            for(int i = 0,size = this.jobLogCheckList.size(); i < size; i++ ){
                FeedAllocCheckEntity checkEntity = this.jobLogCheckList.get(i);
                String serialName = checkEntity.getSerialName();
                Date feedTime = this.findFeedTime(checkEntity.getSerialNo());
                int minute = 0;
                if(null != feedTime){
                    minute = (int) ( (checkEntity.getCreateTime().getTime() - feedTime.getTime())/(1000*60) );
                }
                String checkTimeTemp = serialName + " " + format.format(checkEntity.getCreateTime()) + " " + checkEntity.getUserName();
                String separTimeTemp = minute + "分钟";
                String shrimpFeedQtyTemp = checkEntity.getStatusName();

                checkDTO.append(checkTimeTemp,separTimeTemp,shrimpFeedQtyTemp);

            }
        }
        return checkDTO;
    }

    public Map<String,Object> toZooMap(Map<String,String> logTitle) {
        String strLine = Constants.ENTER_LINE;
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("pondName",pondName);
        //dataMap.put("allocDate",allocDate);

        Map<String, Object> map = new HashMap<>();
        Set<String> allocUserSet = new HashSet<>();
        if(ObjectUtils.isNotEmpty(jobLogZooList)){
            Map<String,Integer> keyMap = new HashMap<>();
            for( JobLogZooVO zooVO : jobLogZooList){
                String feedId = zooVO.getFeedId();
                if( !keyMap.containsKey(feedId)){
                    keyMap.put(feedId,0);
                }
                Integer iKey = keyMap.get(feedId);
                keyMap.put(feedId,iKey++);
                String key = feedId + iKey;

                String feedUserName = zooVO.getFeedUserName();
                feedUserName = StringUtils.isEmpty(feedUserName) ? "": (" " + feedUserName);

                String feedName = zooVO.getFeedName();
                feedName = StringUtils.isEmpty(feedName) ? zooVO.getZooFeedName() : feedName;
                feedName = StringUtils.isEmpty(feedName) ? "": feedName;
                map.put(key,zooVO.getQty() + "斤" + feedUserName + strLine + zooVO.getFeedTime());
                logTitle.put(key,feedName + "/人员");

                if(StringUtils.isNotEmpty(zooVO.getAllocUserName())){
                    allocUserSet.add(zooVO.getAllocUserName());
                }
            }
        }
        String allocUserName = "";
        if(ObjectUtils.isNotEmpty(allocUserSet)){
            for (String userName : allocUserSet){
                allocUserName = allocUserName + userName + strLine;
            }
        }
        this.putAllocDate(map,allocUserName);
        dataMap.putAll(map);
        return dataMap;
    }

    @Data
    private static class CheckDTO{
        private  String checkTime ;
        private String separTime ;
        private String shrimpFeedQty ;

        private List<String> checkTimeList = new ArrayList<>();
        private List<String> separTimeList  = new ArrayList<>();
        private List<String> shrimpFeedQtyList = new ArrayList<>();

        public void append(String checkTime,String separTime,String shrimpFeedQty){
            appendCheckTime(checkTime);
            appendSeparTime(separTime);
            appendShrimpFeedQty(shrimpFeedQty);
        }

        public void appendCheckTime(String append){
            checkTimeList.add( defaultFill(append));
        }
        public void appendSeparTime(String append){
            separTimeList.add( (defaultFill(append)));
        }
        public void appendShrimpFeedQty(String append){
            shrimpFeedQtyList.add( defaultFill(append));
        }
    }


}
